// pages/rent/applyTicket.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '申请发票',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[],
    orderInfo:{
      name: "",
      money: "",
      pay_time: "",
      sn: "",
      type: "",
      type_name: "",
      pay_time: ""
    },
    order_id: 0,
    rent_id: 0,
    type: 0,
    title: "",
    code: "",
    addreess: "",
    tel: "",
    bank: "",
    bank_code: "",
    phone: "",
    email: "",
    orderList:[],
    hasChange: false,
    enableRequest: true,
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    if (app.globalData.rent_id){
      let info = {};
      let order = app.globalData.orderInfo || {};
      for(let k in order){
        info[k] = order[k];
      }
      console.log("onShow",info)
      v.setData({rent_id: app.globalData.rent_id, orderInfo: info});

    }
  },
  radioChange(ev){
    let v = this;
    let val = parseInt(ev.detail.value) || 0;
    v.setData({type: val});
  },
  bindKeyInput(ev){
    let v = this;
    let title = ev.detail.value;
    v.setData({title: title});
  },
  bindKeyInputCode(ev){
    let v = this;
    let code = ev.detail.value;
    v.setData({code: code});
  },
  bindKeyInputPhone(ev){
    let v = this;
    let phone = ev.detail.value;
    v.setData({phone: phone});
  },
  bindKeyInputEmail(ev){
    let v = this;
    let email = ev.detail.value;
    v.setData({email: email});
  },
  doRequestTicket(ev){
    let v = this;
    console.log("requestTicket");
    let url = app.globalData.host + '/api/invoice/rent';
    let data = {token: app.globalData.token};
    if(!v.data.title) {
      wx.showToast({ title: '请完善信息' ,icon:'none'})
      return;
    }
    data.rent_id  = v.data.rent_id;
    data.type     = v.data.type;
    data.title    = v.data.title;
    data.code     = v.data.code;
    data.phone    = v.data.phone;
    data.email    = v.data.email;
    wx.request({
      url: url, data: data,
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          wx.showToast({ title: r.msg || '申请成功',icon:'none'})
          v.setData({enableRequest: false});
        } else {
          wx.showToast({ title: r.msg || '申请失败',icon:'none'})
        }
      }
    })
  },
  onSuccess: function (res) {
    console.log(res.detail);
  },
  onFail: function (res) {
    console.log(res);
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },

  onShareAppMessage: function () {
  }
})